// STKDRV.cpp: implementation of the CStockDriver class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "STKDRV.h"
#include "StockGo.h"
#include "GetSetReg.h"
//#include <shlwapi.h>
//#pragma comment(lib,"..\\NetTS\\Debug\\netts.lib") /*需要加上此行才可以正确link，VC6.0*/ //PathFileExists

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CStockDriver::CStockDriver()
{
	m_pfnSetExtMsg		= NULL;
	m_pfnGetKData		= NULL;
	m_pfnQueryKData		= NULL;
	m_pfnGetMinData		= NULL;
	m_pfnQueryMinData	= NULL;
	m_pfnQueryTradeData = NULL;
	//
	m_pfnRequestStockData = NULL;

	m_pfnStock_Init		= NULL;
	m_pfnStock_Quit		= NULL;
	m_pfnGetTotalNumber = NULL;
	m_pfnSetupReceiver	= NULL;
	m_pfnGetStockDrvInfo= NULL;
	m_hSTKDrv = NULL;
}

CStockDriver::~CStockDriver()
{
	Stock_Quit(m_hWnd);
}

void CStockDriver::GetAdress()
{
	//if(CStockGoApp::m_gbUseExe ==true)	return;

	//GetSetReg
	CFileFind flfd;
	CString m_CurrentWorkDirectory;
	::GetCurrentDirectory(MAX_PATH,m_CurrentWorkDirectory.GetBuffer(MAX_PATH));
	m_CurrentWorkDirectory.ReleaseBuffer();
	m_CurrentWorkDirectory.TrimRight ("\\");
	m_CurrentWorkDirectory.TrimRight ("/");
	m_CurrentWorkDirectory+="\\";
	//
	//AfxMessageBox("试用期已过，若想继续使用，请与系统供应商联系!_0_");//
	//
	if(flfd.FindFile (m_CurrentWorkDirectory+"NetReceiver.dll" ))
	{
		//AfxMessageBox("试用期已过，若想继续使用，请与系统供应商联系!_00_"+m_CurrentWorkDirectory+L"NetReceiver.dll");//
		//////////////
		//HINSTANCE 
		//	m_hSTKDrv = AfxLoadLibrary( m_CurrentWorkDirectory+L"NetReceiver.dll");
		/*if(NULL == m_hSTKDrv)
		{
		AfxMessageBox( "MFC扩展DLL动态加载失败" );
		return;
		}*/
		//return;
		/////////////////////
		m_hSTKDrv = LoadLibrary(m_CurrentWorkDirectory+L"NetReceiver.dll");//E:\StockGo5\DEMO
		//m_hSTKDrv = LoadLibrary("E:\StockGo5\DEMO\NetReceiver.dll");//E:\StockGo5\DEMO
		//AfxMessageBox("试用期已过，若想继续使用，请与系统供应商联系!_001_"+m_CurrentWorkDirectory+"NetReceiver.dll");//
	}
	/*else
	{
	GetSetReg reg;
	char ch[512];
	DWORD ncb = sizeof(ch);
	DWORD typ = REG_SZ;
	if(reg.GetValue ("software\\stockdrv","Driver",typ,(unsigned char *)ch,ncb) == ERROR_SUCCESS)
	{
	ch[511] = 0;
	m_hSTKDrv = LoadLibrary(ch);
	}
	else
	{
	m_hSTKDrv = LoadLibrary("NetReceiver.dll");
	}
	}*/
	//
	//AFX_MANAGE_STATE(AfxGetStaticModuleState());
	//AfxMessageBox("试用期已过，若想继续使用，请与系统供应商联系!_1_");//
	//
	if( !m_hSTKDrv ) 
		m_hSTKDrv = LoadLibrary("NetReceiver.dll");
	if( !m_hSTKDrv ) 
		return;

	m_pfnStock_Init = \
		(int (WINAPI *)(HWND,UINT,int)) GetProcAddress(m_hSTKDrv,"Stock_Init");
	m_pfnStock_Quit = \
		(int (WINAPI *)(HWND)) GetProcAddress(m_hSTKDrv,"Stock_Quit");
	m_pfnGetTotalNumber = \
		(int (WINAPI *)())GetProcAddress(m_hSTKDrv,"GetTotalNumber");
	m_pfnSetupReceiver = \
		(int	(WINAPI *)(BOOL))GetProcAddress(m_hSTKDrv,"SetupReceiver");
	m_pfnGetStockDrvInfo = \
		(DWORD (WINAPI *)(int,void * ))GetProcAddress(m_hSTKDrv,"GetStockDrvInfo");
	m_pfnSetNewsPath = \
		(void (WINAPI*)(LPCTSTR))GetProcAddress(m_hSTKDrv,"SetNewsPath");

	m_pfnSetExtMsg = \
		(int (WINAPI*)(UINT msg))GetProcAddress(m_hSTKDrv,"SetExtMsg");
	m_pfnGetKData = \
		(int (WINAPI*)(LPCSTR scode, time_t mt))GetProcAddress(m_hSTKDrv,"GetKData");
	m_pfnQueryKData = \
		(int (WINAPI*)(LPCSTR scode, int nMarket, time_t mt,int lineType))GetProcAddress(m_hSTKDrv,"QueryKData");
	m_pfnGetMinData = \
		(int (WINAPI*)(LPCSTR scode, time_t mt))GetProcAddress(m_hSTKDrv,"GetMinData");
	m_pfnQueryMinData = (int (WINAPI*)(LPCSTR scode, int nMarket, time_t mt))GetProcAddress(m_hSTKDrv,"QueryMinData");
	m_pfnQueryTradeData = (int (WINAPI*)(LPCSTR scode, int nMarket, time_t mt))GetProcAddress(m_hSTKDrv,"QueryTradeData");
	//
	m_pfnRequestStockData = (int (WINAPI*)( int nDataType, LPCSTR  pStocks, int nSize, int nKType, int nDataCount ))GetProcAddress(m_hSTKDrv,"RequestStockData");

	//AfxMessageBox("试用期已过，若想继续使用，请与系统供应商联系!");//
}

int CStockDriver::Stock_Init(HWND hWnd, UINT uMsg, int nWorkMode)
{
	//if(CStockGoApp::m_gbUseExe ==true)
	//	return 1;

	if( m_hSTKDrv==NULL || m_pfnStock_Init==NULL )
	{
		Stock_Quit(m_hWnd);

		GetAdress();
		if( m_hSTKDrv==NULL || m_pfnStock_Init==NULL)
		{
			return -1;
		}
	}

	m_hWnd = hWnd;
	CString m_CurrentWorkDirectory;
	::GetCurrentDirectory(MAX_PATH,m_CurrentWorkDirectory.GetBuffer(MAX_PATH));
	m_CurrentWorkDirectory.ReleaseBuffer();
	m_CurrentWorkDirectory.TrimRight ("\\");
	m_CurrentWorkDirectory.TrimRight ("/");
	m_CurrentWorkDirectory+="\\";
	int n = m_pfnStock_Init(hWnd,uMsg,m_pfnQueryTradeData?RCV_WORK_RITE_ACTIVE:nWorkMode); 
	SetNewsPath(m_CurrentWorkDirectory+"news");
	return n;
}

int CStockDriver::Stock_Quit(HWND hWnd)
{
	//if(CStockGoApp::m_gbUseExe ==true)
	//	return 1;

	int Ret = -1;
	if( m_hSTKDrv )  
	{
		if(m_pfnStock_Quit )
		{
			Ret = m_pfnStock_Quit(hWnd);
			m_pfnStock_Quit = NULL;
		}
		FreeLibrary(m_hSTKDrv);
		m_hSTKDrv = NULL;
	}
	return Ret;
}

int CStockDriver::GetTotalNumber()
{

	if( !m_pfnGetTotalNumber )
		return -1;
	return( (*m_pfnGetTotalNumber)());
}

int CStockDriver::SetupReceiver(BOOL bSetup)
{
	//if(CStockGoApp::m_gbUseExe ==true)
	//	return 1;
	if( !m_pfnSetupReceiver )
		return -1;
	return( (*m_pfnSetupReceiver)(bSetup));
}

DWORD CStockDriver::GetStockDrvInfo(int nInfo, void *pBuf)
{

	if( !m_pfnGetStockDrvInfo ) 
		return -1;
	return( (*m_pfnGetStockDrvInfo)(nInfo,pBuf));
}
void CStockDriver::SetNewsPath(LPCTSTR lpszNewsPath)
{
	//if(CStockGoApp::m_gbUseExe ==true)
	//	return ;
	if( !m_pfnSetNewsPath )
		return ;

	try
	{
		(*m_pfnSetNewsPath)(lpszNewsPath);
	}
	catch(...)
	{
	}
}
int CStockDriver::SetExtMsg(UINT msg)
{
	if( !m_pfnSetExtMsg )
		return 0;
	return( (*m_pfnSetExtMsg)(msg));
}
int CStockDriver::GetKData(LPCSTR scode, time_t mt)
{
	if( !m_pfnGetKData )
		return 0;
	return( (*m_pfnGetKData)(scode, mt));
}
int CStockDriver::QueryKData(LPCSTR scode, int nMarket, time_t mt,int lineType)
{
	if( !m_pfnQueryKData )
		return 0;
	//int lineType=5;
	return( (*m_pfnQueryKData)(scode, nMarket, mt,lineType));
}
int CStockDriver::GetMinData(LPCSTR scode, time_t mt)
{
	if( !m_pfnGetMinData )
		return 0;
	return( (*m_pfnGetMinData)(scode, mt));
}
int CStockDriver::QueryMinData(LPCSTR scode, int nMarket, time_t mt)
{
	if( !m_pfnQueryMinData )
		return 0;
	m_pfnQueryTradeData(scode, nMarket, mt);//同时查report数据
	return m_pfnQueryMinData(scode, nMarket, mt);
}
//一次请求50个，别多，会出错
int CStockDriver::RequestStockData( int nDataType, LPCSTR pStocks, int nSize, int nKType, int nDataCount )
{
	CString ask="";

	//memcpy( &(ask.m_stocks[0]), pStock, sizeof(TW_STOCK) );
	//	::AfxMessageBox("RequestStockData_00");

	if( !m_pfnRequestStockData )
		return 0;

	//CString scode=pStocks->m_code;
	ask.Format("%s",CString(pStocks));
	//::AfxMessageBox("RequestStockData_"+ask);

	/////////////////////////////////////////
	//CTime	tLatest_mins1 = CTime::GetCurrentTime();
	//CStdioFile  m_fileTxt_nMaxNumber7;
	//CString n_filename_7="股票_RequestStockData_time.txt";
	////CSPTime tLatest_mins1 =CSPTime::GetTradeOffsetToTime(jjk, tLatest_mins.GetTime());
	////if(PathFileExists(n_filename_7)!=1)
	////{
	//m_fileTxt_nMaxNumber7.Open(n_filename_7,CFile::modeCreate | CFile::modeNoTruncate|CFile::modeReadWrite);
	////如果文件事先不存在的话，就需要CFile::modeCreate，否则就不需要。
	//CString cs_nMaxNumber7="";
	//cs_nMaxNumber7.Format("%04d-%02d-%02d %02d:%02d:%02d::#######___%d___#%s########Start\r\n"
	//	,tLatest_mins1.GetYear(),tLatest_mins1.GetMonth(),tLatest_mins1.GetDay()
	//	,tLatest_mins1.GetHour(),tLatest_mins1.GetMinute(),tLatest_mins1.GetSecond(),tLatest_mins1.GetTime(),CString(pStocks));
	//m_fileTxt_nMaxNumber7.SeekToEnd();
	//m_fileTxt_nMaxNumber7.WriteString(cs_nMaxNumber7);
	////--
	//cs_nMaxNumber7.Format("#################################################,%s,%d,lineType:%d___End\n\n",CString(pStocks),QueryKData_i,1);
	////m_fileTxt_nMaxNumber7.SeekToEnd();
	//m_fileTxt_nMaxNumber7.WriteString(cs_nMaxNumber7);
	////m_fileTxt_nMaxNumber7.WriteString("#########################,%s\n",CString(scode));
	////cs_nMaxNumber7.Format("%s\n",asd);
	////--
	////m_fileTxt_nMaxNumber7.WriteString(cs_nMaxNumber7);
	////m_fileTxt_nMaxNumber7.WriteString("#########################\n");
	//m_fileTxt_nMaxNumber7.Close();
	//QueryKData_i++;
	/////////////////////////////////////////

	return m_pfnRequestStockData(  nDataType,  pStocks,  nSize,  nKType,  nDataCount );
	//return 0;
}
